from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_exempt
from users.models import *
import hashlib
@csrf_exempt
def joinus(request):
	if request.method=='POST':
		n=request.POST['name']
		m=request.POST['mobile']
		e=request.POST['email']
		p=request.POST['password']  
		p=hashlib.md5(p.encode("utf")).hexdigest()  
		usersObject=Users(name=n,mobile=m,email=e,password=p)
		context_instance = RequestContext(request)
		usersObject.save()
		context={'userName':usersObject}
		return render_to_response('index.html', context,context_instance=RequestContext(request))
@csrf_exempt
def login(request):
	m = Users.objects.get(email=request.POST['email'])
	p=request.POST['password']
	p=hashlib.md5(p.encode("utf")).hexdigest()  			
	if m.password == p:
		request.session['user_id'] = m.id
		request.session.save()
		return HttpResponseRedirect('/hotel')
	else:
		context={}
		return render_to_response('users/error.html', context,context_instance=RequestContext(request))
@csrf_exempt
def book(request):
	request.session.session_key
	if request.method=='GET':
		bookObject=Book(userId=int(request.session['user_id']),hotelId=request.GET['hid'],numberdays=request.GET['ndays'])
		context_instance = RequestContext(request)
		bookObject.save()
		context={}
		return render_to_response('users/done.html', context,context_instance=RequestContext(request))
@csrf_exempt
def comment(request):
	request.session.session_key
	if request.method=='GET':
		commentObject=Comment(userId=int(request.session['user_id']),hotelId=request.GET['hid'],comment_text=request.GET['comment'])
		context_instance = RequestContext(request)
		commentObject.save()
		context={}
		return HttpResponseRedirect('/hotel')
def actionb(request,hotel_id):
	context={"hotelid":hotel_id}
	return render_to_response('users/book.html', context)
def actionc(request,hotel_id):
	context={"hotelid":hotel_id}
	return render_to_response('users/comment.html', context)
def actions(request,hotel_id):
	comment=Comment.objects.filter(hotelId=hotel_id)
	context={"comments":comment}
	return render_to_response('users/comments.html', context)	
def logout(request):
	del request.session['user_id']
	return HttpResponseRedirect('/index.html')
